#include <iostream>
#include <queue>
using namespace std;
int direc[4][2]={ {0,1},{0,-1},{1,0},{-1,0} };
int a[25][25],n,m;
bool visited[25][25];
struct node {
    int x,y,ste;
};
node start,en;
void BFS() {
    node curr,pre;
    int min=50;
    start.ste=0;
    queue<node> q;
    q.push(start);
    while(!q.empty()) {
        pre=q.front();
        q.pop();
        if (pre.x==en.x && pre.y==en.y) {
            if (min>pre.ste) min=pre.ste;
        }
        for (int i=0; i<4; i++) {
            curr.x=pre.x+direc[i][0];
            curr.y=pre.y+direc[i][1];
            curr.ste=pre.ste;
            if (curr.x<1 || curr.x>n || curr.y<1 || curr.y>m) continue;
            if (i==0 && (a[curr.x][curr.y]==1 || a[curr.x][curr.y]==2 ) ) {
                if (a[curr.x][curr.y]==1 && curr.ste%2==1 || a[curr.x][curr.y]==2 && curr.ste%2==0) curr.y++;
                else {
                    curr.y++;
                    curr.ste++;
                }
            }
            else if (i==1 && (a[curr.x][curr.y]==1 || a[curr.x][curr.y]==2 ) ) {
                if (a[curr.x][curr.y]==1 && curr.ste&2==1 || a[curr.x][curr.y]==2 && curr.ste%2==0) curr.y--;
                else {
                    curr.y--;
                    curr.ste++;
                }
            }
            else if (i==2 && (a[curr.x][curr.y]==1 || a[curr.x][curr.y]==2 ) ) {
                if (a[curr.x][curr.y]==1 && curr.ste%2==0 || a[curr.x][curr.y]==2 && curr.ste%2==1) curr.x++;
                else {
                    curr.x++;
                    curr.ste++;
                }
            }
            else if (i==3 && (a[curr.x][curr.y]==1 || a[curr.x][curr.y]==2 ) ) {
                if (a[curr.x][curr.y]==1 && curr.ste%2==0 || a[curr.x][curr.y]==2 && curr.ste%2==1) curr.x--;
                else {
                    curr.x--;
                    curr.ste++;
                }
            }
            curr.ste++;
            if (curr.x<1 || curr.x>n || curr.y<1 || curr.y>m) continue;
            if (a[curr.x][curr.y]==-1) continue;
            if (visited[curr.x][curr.y]) continue;
            q.push(curr);
            visited[curr.x][curr.y]=true;
        }
    }
    cout<<min<<endl;
}
int main() {
    char ch;
    while(cin>>n>>m) {
        for (int i=1; i<=n; i++) {
            for (int j=1; j<=m; j++) {
                cin>>ch;
                if(ch=='*') a[i][j]=-1;
                if(ch=='.') a[i][j]=0;
                if(ch=='|') a[i][j]=1;
                if(ch=='-') a[i][j]=2;
                if(ch=='S') {
                    start.x=i; start.y=j;
                    a[i][j]=0;
                }
                if(ch=='T') {
                    en.x=i; en.y=j;
                    a[i][j]=0;
                }
            }
        }
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
                visited[i][j]=false;
        BFS();
    }
    return 0;
}
